package api

import (
	"blogdemo/gin/errmsg"
	"blogdemo/gin/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

// Cancel 取消赞/踩接口
func Cancel(c *gin.Context) {
	username := c.PostForm("username")
	author := c.PostForm("author")
	title := c.PostForm("title")

	isExist, attitude := model.FindPreference(username, author, title)
	var msgCode1 int
	var msgCode2 int
	var msgCode3 int
	if !isExist {
		msgCode3 = errmsg.PREFERENCE_NOEXIST
	} else if isExist && attitude == "like" {
		msgCode1 = model.MinusLike(author, title)
		msgCode2 = model.Cancel(username, author, title)
		msgCode3 = errmsg.SUCCESS
	} else if isExist && attitude == "dislike" {
		msgCode1 = model.MinusDislike(author, title)
		msgCode2 = model.Cancel(username, author, title)
		msgCode3 = errmsg.SUCCESS
	}

	if msgCode1 == errmsg.FAIL || msgCode2 == errmsg.FAIL {
		c.JSON(http.StatusOK, gin.H{
			"status":  errmsg.CANCEL_FAIL,
			"message": errmsg.GetErrMsg(errmsg.CANCEL_FAIL),
		})
		c.Abort()
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"status":  msgCode3,
		"message": errmsg.GetErrMsg(msgCode3),
	})
}
